from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_stock.entity.do.info_do import StockInfo
from module_stock.entity.vo.info_vo import InfoModel, InfoPageQueryModel
from utils.page_util import PageUtil


class InfoDao:
    """
    股票基础信息模块数据库操作层
    """

    @classmethod
    async def get_info_detail_by_id(cls, db: AsyncSession, stock_code: str):
        """
        根据股票代码获取股票基础信息详细信息

        :param db: orm对象
        :param stock_code: 股票代码
        :return: 股票基础信息信息对象
        """
        info_info = (
            (
                await db.execute(
                    select(StockInfo)
                    .where(
                        StockInfo.stock_code == stock_code
                    )
                )
            )
            .scalars()
            .first()
        )

        return info_info

    @classmethod
    async def get_info_detail_by_info(cls, db: AsyncSession, info: InfoModel):
        """
        根据股票基础信息参数获取股票基础信息信息

        :param db: orm对象
        :param info: 股票基础信息参数对象
        :return: 股票基础信息信息对象
        """
        info_info = (
            (
                await db.execute(
                    select(StockInfo).where(
                        StockInfo.stock_code == info.stock_code if info.stock_code else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return info_info

    @classmethod
    async def get_info_list(cls, db: AsyncSession, query_object: InfoPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取股票基础信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票基础信息列表信息对象
        """
        query = (
            select(StockInfo)
            .where(
                StockInfo.stock_code.like(f'%{query_object.stock_code}%') if query_object.stock_code else True,
                StockInfo.stock_name.like(f'%{query_object.stock_name}%') if query_object.stock_name else True,
                StockInfo.exchange_id == query_object.exchange_id if query_object.exchange_id else True,
            )
            .order_by(StockInfo.stock_code)
            .distinct()
        )
        info_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return info_list

    @classmethod
    async def add_info_dao(cls, db: AsyncSession, info: InfoModel):
        """
        新增股票基础信息数据库操作

        :param db: orm对象
        :param info: 股票基础信息对象
        :return:
        """
        db_info = StockInfo(**info.model_dump(exclude={}))
        db.add(db_info)
        await db.flush()

        return db_info

    @classmethod
    async def edit_info_dao(cls, db: AsyncSession, info: dict):
        """
        编辑股票基础信息数据库操作

        :param db: orm对象
        :param info: 需要更新的股票基础信息字典
        :return:
        """
        await db.execute(update(StockInfo), [info])

    @classmethod
    async def delete_info_dao(cls, db: AsyncSession, info: InfoModel):
        """
        删除股票基础信息数据库操作

        :param db: orm对象
        :param info: 股票基础信息对象
        :return:
        """
        await db.execute(delete(StockInfo).where(StockInfo.stock_code.in_([info.stock_code])))

    @classmethod
    async def get_all_stock_codes(cls, query_db: AsyncSession):
        """
        获取所有股票代码

        :param query_db: orm对象
        :return: 股票代码列表
        """
        query = select(StockInfo.stock_code)
        result = await query_db.execute(query)
        return result.all()
